#!/bin/sh
#
# Required environment variables:
#
#   DOCSET_INFO_PLIST_PATH - path to (source) Info.plist for documentation set
#   DOCSET_PROJECT_NAME    - project name for documentation set
#	DOCSET_SOURCE_PATH     - path to source header files for documentation set
#   DOCSET_TARGET_PATH     - path to target documentation set
#
# Optional environment variables:
#
#   DOXYCLEAN_PY_PATH      - path to DoxyClean Python script
#					         (default: /usr/local/bin/doxyclean.py)
#   DOXYGEN_BIN_PATH       - path to Doxygen binary
#					         (default: /usr/local/bin/doxygen)
#
CAT=/bin/cat
COPY="/bin/cp -p"
DIRNAME=/usr/bin/dirname
MKDIR="/bin/mkdir -p"
REMOVE="/bin/rm -f"
#
if [ $# -ne 0 ]; then
    echo "Usage: BuildDocSet.sh" 1>&2
    exit 1
fi
#
if [ -z "$DOCSET_PROJECT_NAME" ]; then
    echo "BuildDocSet.sh: environment variable DOCSET_PROJECT_NAME is not set or is empty" 1>&2
    exit 1
fi
#
if [ -z "$DOCSET_SOURCE_PATH" -a -n "${DOCSET_SOURCE_PATH-XXX}" ]; then
    echo "BuildDocSet.sh: environment variable DOCSET_SOURCE_PATH is not set" 1>&2
    exit 1
elif [ ! -e "$DOCSET_SOURCE_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_SOURCE_PATH' does not exist" 1>&2
    exit 1
elif [ ! -d "$DOCSET_SOURCE_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_SOURCE_PATH' is not a directory" 1>&2
    exit 1
elif [ ! -r "$DOCSET_SOURCE_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_SOURCE_PATH' is not readable" 1>&2
    exit 1
fi
#
if [ -z "$DOCSET_TARGET_PATH" -a -n "${DOCSET_TARGET_PATH-XXX}" ]; then
    echo "BuildDocSet.sh: environment variable DOCSET_TARGET_PATH is not set" 1>&2
    exit 1
fi
#
DOCSET_TARGET_DIR_PATH=`$DIRNAME "$DOCSET_TARGET_PATH"`
#
if [ ! -e "$DOCSET_TARGET_DIR_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_TARGET_DIR_PATH' does not exist" 1>&2
    exit 1
elif [ ! -d "$DOCSET_TARGET_DIR_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_TARGET_DIR_PATH' is not a directory" 1>&2
    exit 1
elif [ ! -w "$DOCSET_TARGET_DIR_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_TARGET_DIR_PATH' is not writable" 1>&2
    exit 1
fi
#
if [ -z "$DOCSET_INFO_PLIST_PATH" -a -n "${DOCSET_INFO_PLIST_PATH-XXX}" ]; then
    echo "BuildDocSet.sh: environment variable DOCSET_INFO_PLIST_PATH is not set" 1>&2
    exit 1
elif [ ! -e "$DOCSET_INFO_PLIST_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_INFO_PLIST_PATH' does not exist" 1>&2
    exit 1
elif [ ! -f "$DOCSET_INFO_PLIST_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_INFO_PLIST_PATH' is not a regular file" 1>&2
    exit 1
elif [ ! -r "$DOCSET_INFO_PLIST_PATH" ]; then
    echo "BuildDocSet.sh: '$DOCSET_INFO_PLIST_PATH' is not readable" 1>&2
    exit 1
fi
#
if [ -z "$DOXYGEN_BIN_PATH" -a -n "${DOXYGEN_BIN_PATH-XXX}" ]; then
    DOXYGEN_BIN_PATH=/usr/local/bin/doxygen
fi
#
if [ ! -e "$DOXYGEN_BIN_PATH" ]; then
    echo "BuildDocSet.sh: '$DOXYGEN_BIN_PATH' does not exist" 1>&2
    exit 1
elif [ ! -f "$DOXYGEN_BIN_PATH" ]; then
    echo "BuildDocSet.sh: '$DOXYGEN_BIN_PATH' is not a regular file" 1>&2
    exit 1
elif [ ! -x "$DOXYGEN_BIN_PATH" ]; then
    echo "BuildDocSet.sh: '$DOXYGEN_BIN_PATH' is not executable" 1>&2
    exit 1
fi
#
if [ -z "$DOXYCLEAN_PY_PATH" -a -n "${DOXYCLEAN_PY_PATH-XXX}" ]; then
    DOXYCLEAN_PY_PATH=/usr/local/bin/doxyclean.py
fi
#
if [ ! -e "$DOXYCLEAN_PY_PATH" ]; then
    echo "BuildDocSet.sh: '$DOXYCLEAN_PY_PATH' does not exist" 1>&2
    exit 1
elif [ ! -f "$DOXYCLEAN_PY_PATH" ]; then
    echo "BuildDocSet.sh: '$DOXYCLEAN_PY_PATH' is not a regular file" 1>&2
    exit 1
elif [ ! -x "$DOXYCLEAN_PY_PATH" ]; then
    echo "BuildDocSet.sh: '$DOXYCLEAN_PY_PATH' is not executable" 1>&2
    exit 1
fi
#
TMP_DIR=`cd "$TMPDIR" && pwd`/docset-$$
#
DC_TMP_DIR="$TMP_DIR"/doxyclean		# holds DoxyClean-generated files
DG_TMP_DIR="$TMP_DIR"/doxygen		# holds Doxygen-generated files
DS_TMP_DIR="$TMP_DIR"/docset		# holds documentation set in progress
#
# Create temporary directory and various subdirectories:
#
$MKDIR "$DC_TMP_DIR"
$MKDIR "$DG_TMP_DIR"
$MKDIR "$DS_TMP_DIR"/Contents/Resources/Documents
#
# Generate Doxygen configuration:
#
DOXYGEN_CFG="$DG_TMP_DIR"/Doxygen.cfg
#
$DOXYGEN_BIN_PATH -s -u - << EOF > "$DOXYGEN_CFG"
FILE_PATTERNS        = *.h
GENERATE_HTML        = NO
GENERATE_LATEX       = NO
GENERATE_XML         = YES
INPUT                = "$DOCSET_SOURCE_PATH"
OUTPUT_DIRECTORY     = "$DG_TMP_DIR"
PROJECT_NAME         = "$DOCSET_PROJECT_NAME"
REPEAT_BRIEF         = NO
WARN_IF_UNDOCUMENTED = NO
EOF
#
# Run Doxygen:
#
$DOXYGEN_BIN_PATH "$DOXYGEN_CFG"
#
# Run DoxyClean:
#
$DOXYCLEAN_PY_PATH -n "$DOCSET_PROJECT_NAME"   \
                   -i "$DG_TMP_DIR"/xml \
                   -o "$DC_TMP_DIR"     \
                   -p
#
# Expand environment variables in Info.plist for documentation set:
#
`$DIRNAME "$0"`/ExpandEnvVars.sh < "$DOCSET_INFO_PLIST_PATH" > "$DS_TMP_DIR"/Contents/Info.plist
#
# Generate Nodes.xml for documentation set:
#
NODES_XML="$DS_TMP_DIR"/Contents/Resources/Nodes.xml
#
$CAT << EOF > "$NODES_XML"
<?xml version="1.0" encoding="UTF-8" ?>
<DocSetNodes version="1.0">
    <TOC>
        <Node type="folder">
            <Name>Root</Name>
            <Path>index.html</Path>
        </Node>
    </TOC>
</DocSetNodes>
EOF
#
# Generate Tokens.xml for documentation set:
#
#TOKENS_XML="$DS_TMP_DIR"/Contents/Resources/Tokens.xml
#
#
# Copy in document content for documentation set:
#
$COPY -R "$DC_TMP_DIR"/html/ "$DS_TMP_DIR"/Contents/Resources/Documents
#
# Generate indexes for documentation set:
#
XCODE_DIR=`/usr/bin/xcode-select -print-path`
#
$XCODE_DIR/usr/bin/docsetutil index "$DS_TMP_DIR"
#
# Remove unused files from documentation set:
#
$REMOVE "$NODES_XML"
#$REMOVE "$TOKENS_XML"
#
# Copy documentation set to final destination and clean up:
#
$MKDIR "$DOCSET_TARGET_PATH"
$COPY -R "$DS_TMP_DIR"/ "$DOCSET_TARGET_PATH"
#
$REMOVE -R "$TMP_DIR"
#
exit 0
